#! /usr/bin/env python
#coding:utf8

import xlrd
import csv
import os
 
# import sys

# reload(sys)
# sys.setdefaultencoding('utf-8')

# print sys.getdefaultencoding()

# switch[case](arg)

<<<<<<< HEAD
def sheet_to_csv(book, sheetid, writer, formats):
    sheet = book.sheet_by_index(sheetid)
    for i in xrange(sheet.nrows):
        row = [""] * sheet.ncols
        cells = sheet.row(i)
        for j in xrange(sheet.ncols):
            cell = cells[j]
        if cell.ctype == xlrd.XL_CELL_TEXT:
            cval = cell.value.encode('utf-8')
        elif cell.ctype == xlrd.XL_CELL_NUMBER:
            if cell.xf_index != None:
                a_fmt = formats[book.xf_list[cell.xf_index].format_key]
                if a_fmt:
                    cval = format_number(cell.value, a_fmt, "", ".")
                elif cell.value == int(cell.value):
                    cval = int(cell.value)
                else:
                    cval = "%s" % cell.value
        elif cell.ctype == xlrd.XL_CELL_DATE:
            try:
                cval = xlrd.xldate_as_tuple(cell.value, book.datemode)
            except xlrd.XLDateError:
                e1, e2 = sys.exc_info()[:2]
                cval = "%s:%s" % (e1.__name__, e2)

        else:  # XL_CELL_EMPTY, XL_CELL_ERROR, XL_CELL_BLANK
            cval = ""
        row[j] = cval
        print row[i]
=======
def Excel2CSV(ExcelFile, CSVFile):
     print " ExcelFile " + ExcelFile
     print " CSVFile " + CSVFile
     workbook = xlrd.open_workbook(ExcelFile)
     worksheet = workbook.sheet_by_index(0)
     csvfile = open(CSVFile, 'wb')
     wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL)

     for rownum in xrange(worksheet.nrows):
         wr.writerow(
             list(x.encode('utf-8') if type(x) == type(u'') else x
                  for x in worksheet.row_values(rownum)))

     csvfile.close()
>>>>>>> 1436e63e91bed0aad4153e33c53563590e658a0d

for curPath in os.listdir("."):
    # if os.path.isfile(curPath) and os.path.splitext(curPath)[1] == '.xlsx':
    #     wb = open_workbook(curPath)
    #     outputTxt(wb.sheet_by_index(0), os.path.splitext(curPath)[0])
    if os.path.isfile(curPath) and os.path.splitext(curPath)[1] == '.xls':
<<<<<<< HEAD
        wb = open_workbook(curPath)
        sheet_to_csv(wb, 0, os.path.splitext(curPath)[0])
=======
        wb = xlrd.open_workbook(curPath)
        sheet = wb.sheet_by_index(0)
        for i in range(sheet.nrows):
            print sheet.row_values(i)
        # outputTxt(wb.sheet_by_index(0), os.path.splitext(curPath)[0])
>>>>>>> 1436e63e91bed0aad4153e33c53563590e658a0d


# eror1
# values = [50101.0, u'\u89c2\u661f\u8005', u'2|2|2|0']
# curStr = ';'.join(values)
# TypeError: sequence item 0: expected string, float found

# error2
# values = [50101.0, u'\u89c2\u661f\u8005', u'2|2|2|0']
# curStr = ';'.join(str(v) for v in values)
# UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
# cause u'\u89c2\u661f\u8005

# OK
# values = [50101.0, u'\u89c2\u661f\u8005', u'2|2|2|0']
# curStr = ';'.join(unicode(v) for v in values).encode('utf-8')

#最终抛弃了这个方案

# Excel treats all numbers as floats.
# 最终还是不行的 比如你填个 string 1 => 结果就变成 string 1.0了 这样不好 
# 但是如果统一成int 1 其实也不好 为什么有时候这里就是string 到时候说不定就要改的说
# 哎 抛弃这个方案了

# http://stackoverflow.com/questions/8825681/integers-from-excel-files-become-floats
# http://uucode.com/blog/2013/10/22/using-xlrd-and-formatting-excel-numbers/


# http://xlrd.readthedocs.io/en/latest/api.html#module-xlrd
# https://github.com/hempalex/xls2csv/blob/master/xls2csv.py
# https://pypi.python.org/pypi/xlrd

# https://git.oschina.net/youke/xlsxtools.git